package com.pushtalk;


import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.pushtalk.utils.ConfigUtil;

public class SqlConnection {
	protected static final Logger LOG = LoggerFactory.getLogger(SqlConnection.class);
	public static Connection getPushConnection(){
		Connection conn= null;
		try {
			Properties properties = ConfigUtil.getProperties();	
			
			String driver = properties.getProperty("dbDriver").trim();
			String URL = properties.getProperty("dbConnectionUrl").trim();
			String user = properties.getProperty("dbUser").trim();
			String password = properties.getProperty("dbPassword").trim();
			
			Class.forName(driver);           
			conn = DriverManager.getConnection(URL,user,password);            
		} catch (FileNotFoundException e1) {
			LOG.error("配置文件未找到：", e1);
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			LOG.error("配置文件未找到：", e1);
			e1.printStackTrace();
		} catch (SQLException e) {
			LOG.error("SQLException", e);
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			LOG.error("ClassNotFoundException", e);
		}
		return conn;
	}
	
	public static Connection GetDBConnection(String driver,String connectionUrl,String user,String password) 
	{
		Connection conn= null;
		try{  
			try{  
				Class.forName(driver).newInstance();
			}catch(Exception e){  
				e.printStackTrace();  
				LOG.error("SQLException", e);
			}  
			conn = DriverManager.getConnection(connectionUrl,user,password);  
			return conn;

		}catch(Exception e){  
			e.printStackTrace();  
			LOG.error("SQLException", e);
			return null;
		}  

	}
}
